﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SagiShopLibrary.BusinessObjects;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Localization;
using DevExpress.XtraEditors.DXErrorProvider;

namespace SagiShop.Modules.ModuleKho
{
    public partial class ScreenLoaiHangHoa : DevExpress.XtraEditors.XtraForm
    {
        private int idHangHoa;
        
        public ScreenLoaiHangHoa()
        {
            InitializeComponent();
            this.SetStatusButtons();
            this.AddEvents();
            this.CreateColumnGridview();
            this.LoadRecords();

            GridLocalizer.Active = new MyGridLocalizer();
        }
        private void AddEvents()
        {
            btnThemMoi.Click += new EventHandler(btnThemMoi_Click);
            btnDieuChinh.Click += new EventHandler(btnDieuChinh_Click);
            btnLuu.Click += new EventHandler(btnLuu_Click);
            btnHuy.Click += new EventHandler(btnHuy_Click);
            txtLoaiHangHoa.Validated += delegate(object sender, EventArgs e)
                                        {
                                            bool status = dxValidationProvider1.Validate();
                                            status = true;
                                        };
            txtLoaiHangHoa.Validating += delegate(object sender, CancelEventArgs e)
                                         {
                                             dxValidationProvider1.Validate();
                                         };
            gridViewLoaiHangHoa.RowClick += new DevExpress.XtraGrid.Views.Grid.RowClickEventHandler(row_Click);

            var notEmptyValidationRule = new ConditionValidationRule()
            {
                ConditionOperator = ConditionOperator.IsNotBlank,
                ErrorText = "Bạn Chưa Nhập Tên Loại Hàng Hóa"
            };
            dxValidationProvider1.SetValidationRule(txtLoaiHangHoa, notEmptyValidationRule);

        }



        private void row_Click(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            bool status = dxValidationProvider1.Validate();
            status = true;
            if (e.Clicks > 0)
            {
                btnThemMoi.Enabled = false;
                btnDieuChinh.Enabled = true;
                txtLoaiHangHoa.Enabled = false;
                txtMoTa.Enabled = false;
                idHangHoa = (Int32)gridViewLoaiHangHoa.GetRowCellValue(e.RowHandle, "ID");
                txtLoaiHangHoa.Text = gridViewLoaiHangHoa.GetRowCellValue(e.RowHandle, "Tên Loại Hàng Hóa").ToString();
                txtMoTa.Text = gridViewLoaiHangHoa.GetRowCellValue(e.RowHandle, "Mô Tả").ToString();
            }
        }
        #region su kien cho button


        private void btnHuy_Click(object sender, EventArgs e)
        {
            bool status = dxValidationProvider1.Validate();
            status = true;
            this.EmptyTextbox();
            if (btnDieuChinh.Enabled)// tra ve true
            {
                txtMoTa.Enabled = true;
                txtLoaiHangHoa.Enabled = true;
                btnDieuChinh.Enabled = false;
                btnThemMoi.Enabled = true;
                btnLuu.Enabled = false;
                EmptyTextbox();
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            bool status = dxValidationProvider1.Validate();
            try
            {
                if (status)
                {
                    var loaiHangHoa = txtLoaiHangHoa.Text.Trim();
                    var moTa = txtMoTa.Text.Trim();
                    try
                    {
                        BOLoaihanghoa boLoaiHH = new BOLoaihanghoa() { IdLhh = idHangHoa, TenLhh = loaiHangHoa, MotaLhh = moTa };
                        boLoaiHH.Update();
                        btnDieuChinh.Enabled = false;
                        btnLuu.Enabled = false;
                        btnThemMoi.Enabled = true;
                        this.EmptyTextbox();
                        txtLoaiHangHoa.Focus();
                        this.LoadRecords();
                    }
                    catch
                    {

                        XtraMessageBox.Show("Cập Nhật Không Thành Công");
                    }
                }
                else
                {
                    dxValidationProvider1.Validate();
                    txtLoaiHangHoa.Focus();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btnDieuChinh_Click(object sender, EventArgs e)
        {
            btnThemMoi.Enabled = false;
            txtLoaiHangHoa.Enabled = true;
            txtMoTa.Enabled = true;
            btnLuu.Enabled = true;
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            try
            {
                if (!String.IsNullOrEmpty(txtLoaiHangHoa.Text))
                {
                    var loaiHangHoa = txtLoaiHangHoa.Text.Trim();
                    var moTa = txtMoTa.Text.Trim();
                    try
                    {
                        BOLoaihanghoa boLoaiHH = new BOLoaihanghoa() { TenLhh = loaiHangHoa, MotaLhh = moTa, TtLhh = true };
                        boLoaiHH.SaveNew();
                        this.EmptyTextbox();
                        txtLoaiHangHoa.Focus();
                        this.LoadRecords();
                    }
                    catch
                    {

                        XtraMessageBox.Show("Thêm Mới Không Thành Công");
                    }
                }
                else
                {
                    dxValidationProvider1.Validate();
                    txtLoaiHangHoa.Focus();
                }
            }
            catch
            {
                throw;
            }
        }

        private void SetStatusButtons()
        {
            btnThemMoi.Enabled = true;
            btnLuu.Enabled = false;
            btnDieuChinh.Enabled = false;
        }
        #endregion
        #region su kien cho textbox
        private void EmptyTextbox()
        {
            txtLoaiHangHoa.Text = string.Empty;
            txtMoTa.Text = string.Empty;
        }
        #endregion
        #region khoi tao cho datatable
        /// <summary>
        /// tao du lieu cho row
        /// </summary>
        public void LoadRecords()
        {
            try
            {
                var table = new DataTable();
                table.Columns.Add("ID", typeof(Int32));
                table.Columns.Add("Tên Loại Hàng Hóa", typeof(string));
                table.Columns.Add("Mô Tả", typeof(string));

                var boLHHList = BOLoaihanghoa.LoaihanghoaCollection();
                foreach (var boLHH in boLHHList)
                {
                    table.Rows.Add(boLHH.IdLhh, boLHH.TenLhh, boLHH.MotaLhh);
                }

                this.bindingSource1.DataSource = table;
                this.gridControlLoaiHangHoa.DataSource = this.bindingSource1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Không Hiển Thị Được");
                return;
            }
        }
        /// <summary>
        /// tao title cho colum cua grid view
        /// </summary>
        private void CreateColumnGridview()
        {
            GridColumn colId = new GridColumn();
            colId.FieldName = colId.Caption = "ID";
            colId.VisibleIndex = 0;
            colId.Visible = false;
            gridViewLoaiHangHoa.Columns.Add(colId);

            GridColumn colName = new GridColumn();
            colName.FieldName = colName.Caption = "Tên Loại Hàng Hóa";
            colName.VisibleIndex = 1;
            gridViewLoaiHangHoa.Columns.Add(colName);

            GridColumn colMoTa = new GridColumn();
            colMoTa.FieldName = colMoTa.Caption = "Mô Tả";
            colMoTa.VisibleIndex = 2;
            gridViewLoaiHangHoa.Columns.Add(colMoTa);

            gridViewLoaiHangHoa.OptionsFind.AlwaysVisible = true;
            gridViewLoaiHangHoa.OptionsFind.FindNullPrompt = "Nhập Nội Dung Cần Tìm Kiếm.....";
        }
        #endregion
    }
    public class MyGridLocalizer : GridLocalizer
    {
        public override string GetLocalizedString(GridStringId id)
        {

            if (id == GridStringId.FindControlFindButton)
                return "Tìm Kiếm";
            if (id == GridStringId.FindControlClearButton)
                return "Làm Lại";
            return base.GetLocalizedString(id);
        }
    }
}